package com.example.newsmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.newsmanager.dao.CollectMapper;
import com.example.newsmanager.entity.Collect;
import com.example.newsmanager.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public void addCollect(Collect collect){
        Date date = new Date();
        collect.setCollectDate(date);
        collectMapper.insert(collect);
    }
    @Override
    public void deleteCollect(Collect collect,String userId){
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id",collect.getNewsId())
                    .eq("user_id",userId);
        collectMapper.delete(queryWrapper);
    }

    @Override
    public Boolean isCollected(Collect collect,String userId){
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id",collect.getNewsId())
                    .eq("user_id",userId);
        if(collectMapper.selectOne(queryWrapper)!=null){
            return true;
        }
        else {
            return false;
        }
    }


}
